package com.boot.mall.order.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117671198";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCCSF6BkDKWDWdsIolOLo81SdVVMXdgOZiDnHF95xW0pisBk1Kjk03kuaXL8sv2FP3k/VkRWOKFEn5cz35v5w879eqpEOnLo+k+DyszU4E26ZmaZ02sJ+0hvYApKxPXD12UCha58o95ZuDa8Yx34JO1glHOACeTHIkwvvdceqmWB4t98oi484uLGV8RuhghSFGFr3NXcMRHTf9uT1FxQk1WbL4ZAM9lICufq/BNGt5WJIkKbmWQ7Gf6MS4G01O6h0uvCbDLinbrj4Da1S+LFdxFJ+a6HGTfPLfKUP51KPVbhemTWhcBoLe3OOsLIyvFsk+UoSjY/cbtlmyDaoGBzqYTAgMBAAECggEAKwI/CKCUwopsQPtpvoCKUDoujfBFi2OYIKKNqNWXicptWhjLaCqhxUcwH5LomDU319d3Bs4EAZAoXPU2tTRfCBlXvTZTKCHVRuJ1DX2co4ccaSd/Mb0AgGnxnCWXaCCvcMaf2ATmLlqzEjQ5ff2KZZo+7TWJ2vvJomj4efQYdelLf/PY8P9b5+Ak0YIwXERlsddTxLWcfcIpa8Vke31g+LnbU4kzYKVzpuACz/KnsRNeyaJe/nwWHpG3IcG852nBFjf2H4y+Ar0Cw+4LhYeggXRHmScQI6BUoPlsEvI5htRaLBtzqBelyqDxFFvmw6VvKJflo7jeMchBnGdB3h1A8QKBgQD3sDs65tNP6clE6kF5NsFkUd8+ziDM/V9fiZGVEOzGy268Mz2RzEVvhhL+iVuzpGWrqdwKHvsJYwX8WJMAer/AYqf9XC3qZECTDPmcIjs3fR9KrsF3hMmBR51+CmfI3HqcPNXd2ZHiTGgxF32YLE7bU1/06gQ2JW7VPwU0CTRXSQKBgQCGp5A4zswgnQRdZD5kyeIxReGsxI3ZN7O7MTrT8snuE9/qkhZp3W1EYom+O9rP6s1QZxPq5/wVL0SXLQEkiC0D6L/6yrYdnXuFNDZvAWYyp3wGXiD3na2Cb+8XJi5A8Uc8DorycB2H/iWtN1qFOlKJI6YP1iTWsYZ17Tzd/bUGewKBgQDJNLoI0TXbdvjvKLTjON3FFtUZee0fWLw/Kcu/oi4WF2/vJljftWa6f9myN/bsaaHF34hGGICpUW2tXm6cNxrJt9rrcBREkgf67NFgSlZ8bwIacvfqlU3z3S9NEjrY6nIXtTuuqm1dvd/t8T0vft7YQFR06h1dtu7f///EE64XMQKBgDUlr/PsE3+P2egup73KPR/rkB5L2hzxMlxhGvIK8w6oA4onLPeN8RG7vtg58ax85ZqlhKQD+mg2JuFwXvIXWYUgeZ24ItdfkCBMMmvv5nF5z6cHwgVZ0+EW4wyFAl6ZnN8kck0sWUuS0TYkEfNhyK/eRS4c/J9MpGCpSYvpFmk/AoGBAJKwySMjzvrb/Ix2doCNz6qkxHxZd08eVxbgnFSq84gJrfrnfAGYgDQjyR+9xkDjzdEe+E6pvmWJeP+c1Nt9+zNFw2nXsVHDXlQMzlpLsagnUmyhKvVDtblxWnUC/yrdsaobKRAOIMayGILVHiFUK6uhTQwp9O7xC1tZWo1IxuVT";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp/kSDQcHohTPCqiCmA+W7STzuKDCkqko2CBEema/cHyzmzHSzcq+L6BgApVy8n4jKkHifTT96dWHrkNcQq2bDnqcIlKUQORB9c/DrpnvjHpf1NRn3dkKo7b9qB0z0FR3iVBkdU0JnPnqeBCWel4D/MeVWEOie+JsPMqU/z0l3cvSnrpHjlvcUr/NkYKvg1jnSOWgPaDulvwy3zpDjfeO8GlOZETEsD2GKIb+KjJ2dHonx0XOjW+5aS7hioijbfG/VU0dk/KCQeVkCNUTO+pO3bmHF2tigEXeBOOjeSO73BbKm6xC29ksoxPT7Z9HXebXX3rcfofpsQ8OQBvT5A75+wIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://38y81464z0.oicp.vip/alipay/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://order.guli.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 自动收单时间
    private String timeout = "10m";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                +"\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
//        System.out.println("支付宝的响应："+result);

        return result;

    }
}
